<?php

namespace app\common\model\transit;

use app\api\controller\Pub;
use app\common\model\user\User;
use think\Model;

class Transport extends Model
{
    // 设置当前模型对应的完整数据表名称
    protected $table = 'transport';
    protected $pk    = 'TranSportId';

    const STATEINT    = 1; //初始化
    const STATEOK     = 1; //没问题的
    const STATELOCK   = 2; //锁定
    const STATEDELETE = 404; //被删除的

    public function company()
    {
        return $this->hasOne('Transitcompany', 'TransitCompanyId', 'TransitCompanyId');
    }

    /**
     * 状态分类
     * @param  string $value [description]
     * @return [type]        [description]
     */
    public static function getStatusText($value = '')
    {
        $status = [-1 => '全部工具', 1 => '正常使用', 2 => '隐藏工具'];
        if ($value !== '') {
            return $status[$value];
        }
        return $status;
    }
    /**
     * 获取imei列表，默认,拼接成字符串
     * @return [string] [imei数组，以,拼接]
     */
    public static function get_imei_list(){
        $list = Transport::where('State',Transport::STATEOK)->column('IMEI');
        $str = '';
        foreach ($list as $key => $value) {
            if(!empty($value)){
                $str = $str . $value;
            }
            if(!empty($list[$key+1])){
                $str = $str .',';
            }
        }
        return $str;
    }
    /**
     * 创建企业信息
     * @param  [type] $data [description]
     * @return [type]       [description]
     */
    public static function user_create($data, $type = 'tran')
    {
        $userlogininfo = Pub::getuserid();
        $userid        = $userlogininfo['msg'];

        if ($type == 'tran') {
            $tc   = Transitcompany::user_getinfo();
            $tcid = $tc['TransitCompanyId'];
        } else if ($type == 'mana') {
            $tcid = $data['id'];
        }
        //判断是否存在
        $sb = Transport::where('BrandNumber', $data['BrandNumber'])->where('Type', $data['Type'])->find();
        if (!empty($sb)) {
            return moerr('此运输工具已存在。');
        }

        $tsp              = new Transport();
        $tsp->BrandNumber = $data['BrandNumber'];
        $tsp->Type        = $data['Type'];

        $tsp->TransitCompanyId = $tcid;
        $tsp->Img              = $data['Img'];
        $tsp->Note             = $data['Note'];

        $tsp->State = Transport::STATEINT;
        $tsp->save();
        return $tsp->TranSportId;
    }

    /**
     * 获取信息
     * @param  [type] $id [description]
     * @return [type]     [description]
     */
    public static function user_getinfo($cid = '', $userid = '', $hidden = ['ReInfo'])
    {

        if (!empty($cid)) {
            $data = Transitcompany::where('TransitcompanyId', $cid)->find();
        } else {
            if (empty($userid)) {
                $userlogininfo = Pub::getuserid();
                $userid        = $userlogininfo['msg'];
            }
            $data = Transitcompany::where('UserId', $userid)->find();
        }
        if (empty($data)) {
            return false;
        }
        $data->hidden($hidden);
        return $data;
    }

    /**
     * 用户修改企业信息
     * @param  [type] $data [description]
     * @return [type]       [description]
     */
    public static function user_reinfo($id, $data, $type = 'tran')
    {
        $company = Transport::get($id);
        if ($type == 'tran') {
            $sb = Transitcompany::get($company['TransitCompanyId']);
            if ($sb['UserId'] !== Pub::userid()) {
                return moerr('没有修改权限');
            }
        }
        $reinfo = empty($company->OperationLog) ? '' : $company->OperationLog . "\n";

        $userinfo = User::getinfo();
        $reinfo   = $reinfo . '【' . $userinfo->Name . '|' . $userinfo->UserId . '】在' . date('Y-m-d H:i', time()) . '进行了信息调整：';
        $nreinfo  = '';
        foreach ($data as $key => $value) {
            $sb = $company->getfiledname($key);
            if (empty($sb)) {
                continue;
            }
            if (!empty($value) && $company->$sb !== $value) {
                $nreinfo = $nreinfo . '字段【' . $company->getfiledname($key, 2) . '】由【' . $company->$sb . '】修改为【' . $value . '】;' . "\n";
                //输入来的参数名非字段名需要进行替换
                $company->$sb = $value;
            }
        }
        if (!empty($nreinfo)) {
            $reinfo                = $reinfo . "\n" . $nreinfo;
            $company->OperationLog = $reinfo;
            $company->save();
        }
    }

    /**
     * 获取订单列表
     * @param  [type] $data    [主数据]
     * @param  string $type    [查询类型/hawa/buss]
     * @param  [type] $is_page [是否分页显示]
     * @return [type]          [description]
     */
    public static function usergetlist($data, $type = 'tran', $is_page = true)
    {
        $rdata    = Transport::where('State', '<>', Transport::STATEDELETE);
        $userinfo = User::getinfo();
        if ($type == 'tran') {
            //有企业id直接设置企业id为条件 没有则判断是否为管理者
            if (empty($data['tid'])) {
                $company = Transitcompany::user_getinfo();
                $rdata   = Transport::where('TransitCompanyId', $company['TransitCompanyId']);
            } else {
                $rdata = Transport::where('TransitCompanyId', $data['tid']);
            }
        } else if ($type == 'buss') {
            $rdata         = Transport::where('State', Transport::STATEOK);
            $data['order'] = 'TransitCompanyId desc';
        } else if ($type == 'ware') {
            $rdata         = Transport::where('State', Transport::STATEOK);
            $visible       = ['BrandNumber', 'Img', 'Note', 'TranSportId', 'Type','company.Name','TransitCompanyId'];
            $data['order'] = 'TransitCompanyId desc';
            $rdata         = $rdata->with('company');
        } else {
            return moerr('缺少参数');
        }

        $option = come_model_option($data, $rdata, $is_page);

        $rdata = $option['rdata'];
        $data  = $option['data'];

        if (!empty($data['field'])) {
            $rdata = $rdata->field($visible);
        }else{
             //隐藏修改日志
            $rdata = $rdata->hidden(['OperationLog']);
        }
        $rdata = $rdata->select();

        if (empty($data['field']) && !empty($visible)) {
            $rdata = $rdata->visible($visible);
        }

        $ndata['list']      = $rdata->toArray();
        $ndata['statetext'] = Transport::getStatusText();

        if ($type == 'buss') {
            $ndata['list'] = arr_id_formate($ndata['list'], 'TransitCompanyId');
        }

        // $goodid = get_array_id($ndata,'GoodId','list','listinfo');
        if ($is_page) {
            $ndata['pageinfo'] = ['pageindex' => $data['pageindex'], 'pagemax' => $data['count']];
        }
        return $ndata;
    }

    public static function getfiledname($str, $type = 1)
    {
        $filename = ['BrandNumber' => '牌号',
            'Type'                     => '运输工具类型', 'Img' => '图片信息',
            'Note'                     => '备注信息'];
        if ($type == 1) {
            if (empty($filename[$str])) {
                return false;
            }
            return $str;
        } else {
            if (empty($filename[$str])) {
                return false;
            }
            return $filename[$str];
        }
    }

}
